
package andre.grids.db;

import andre.grids.filesystem.server.configuration.*;
import com.db4o.*;

/**
 *
 * @author andre
 */
public class Database {
    private ObjectContainer objects;
    
    public Database(String dbFile)
    {
        try
        {
            objects = Db4o.openFile(dbFile);            
        }
        catch(Exception e)
        {
            andre.grids.util.log.Log.logException(e, this, true);            
        }
    }
    
    public ObjectContainer getObjects()
    {
        return objects;
    }
    
    public void closeDb()
    {
        try
        {
            objects.close();
        }
        catch(Exception e)
        {
            andre.grids.util.log.Log.logException(e, this);
        }
    }
    
    public void storeObjectTree(Object o)
    {
        objects.set(o);
        objects.commit();
    }
    
    public void deleteObject(Object o) {
        objects.delete(o);
        objects.commit();
    }
    
    private static Database gridFsDbInstance;
    
    public static Database getGridFsDb()
    {
        if (Database.gridFsDbInstance == null)
        {
            gridFsDbInstance = new Database(ConfigurationManager.getDbConfigInstance().getValue(ServerConfigurationProps.DB_DATA_FILE));
        }
        return gridFsDbInstance;
    }
}
